<?php

/**
 * 自动生成 SysUserController
 * 
 * Apps/Backend/Controller/SysUserController.class.php
 *  Zix @ 2015-06-24
 */

namespace Backend\Controller;

use Common\Service\SysUserService;
use Common\Service\SysDictionaryService;
use Common\Service\SysRoleService ;
use Common\Service\SysGroupService;
use Common\Service\AuthService;

class SysUserController extends BackendController {

  public function index() {
    $action = I('get.action', 'read');

    $data = $this->_init_data();
    $data['module_name'] = '系统用户';
    $data['module_url'] = U( $this->base_path . 'index');

    $SysUser = SysUserService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => $this->menu_id ,
      'module_url' => U($this->base_path . 'index'),
      'read_url'   => U($this->base_path . 'read' ),
      'create_url' => U($this->base_path . 'create'),
      'update_url' => U($this->base_path . 'update'),
      'delete_url' => U($this->base_path . 'delete'),
      'get_group_url' => U($this->base_path . 'get_group') . '/',
      'reset_pwd_url' => U($this->base_path . 'reset_pwd') . '/',
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $SysUser->get_default_row() 
    );

    $SysDictionary = SysDictionaryService::instance();
    $SysRole = SysRoleService::instance() ;
    $data['param'] = array(
      'status' => $SysDictionary->get_sys_user_status() ,
      'yesno' => array( 0 => '否' , 1 => '是' ) ,
      'role_data' => $SysRole->get_by_cond(array(
        'rank' => $this->auth['role_rank'],
        'sys_type' => self::SYS_TYPE ,
      ))
    );

    $this->display_with_layout($data, $this->controller .':index');
  }

  function read() {
    $post = I('post.');

    $config = array(
      'page' => $post['page'],
      'page_size' => $post['page_size'],
      'keyword' => $post['keyword'],
      'time_from' => $post['time_from'],
      'time_to' => $post['time_to'],
      'sys_type' => 2 ,
      'status' => $post['status'],
      'role_rank' => $this->auth['role_rank'] ,
      'role_id' => $post['role_id'],
      'sort' => empty($post['sort']) ? 'id' : $post['sort'],
      'order' => empty($post['order']) ? 'DESC' : $post['order'],
    );

    $SysUser = SysUserService::instance();
    $data['rows'] = $SysUser->get_by_cond($config);

    $config['count'] = TRUE;
    $data['total'] = $SysUser->get_by_cond($config);

    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    $post['sys_type'] = 2 ;
    $post['password'] = md5('123456');
    $post['token'] = '';
    
    $role_id = $post['role_id'];
    $group_id = $post['group_id'];
    
    unset($post['role_id']);
    unset($post['group_id']);
    
    $SysUser = SysUserService::instance();
    $ret = $SysUser->create($post);
    
    if ( !$ret['status'] ) {
      $this->ajaxReturn($ret);
    }
    
    $user_id = $ret['id'];
    $SysRole = SysRoleService::instance();
    $SysRole->create_user_role( $user_id , $role_id) ;
    
    $SysGroup = SysGroupService::instance();
    $SysGroup->create_user_group( $user_id , $group_id) ;
    
    
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    $role_id = $post['role_id'] ;
    unset($post['role_id']);
    
    $group_id = $post['group_id'] ;
    unset($post['group_id']);

    $SysUser = SysUserService::instance();
    $ret = $SysUser->update($id, $post);
    
    $SysRole = SysRoleService::instance();
    $role_ret = $SysRole->update_user_role( $id , $role_id) ;
    
    $SysGroup = SysGroupService::instance();
    $group_ret = $SysGroup->update_user_group( $id , $group_id) ;

    if ( $ret['status'] || $role_ret['status'] || $group_ret['status']) {
      $this->ajaxReturn(ajax_arr('编辑成功' , TRUE ));
    } else {
      $this->ajaxReturn(ajax_arr('编辑失败' ));
    }
  }

  function delete() {
    $ids = I('post.ids');
    
    $SysRole = SysRoleService::instance();
    $SysRole->delete_user_role_by_user_id($ids);
    
    $SysGroup = SysGroupService::instance();
    $SysGroup->delete_user_group_by_user_id($ids);
    
    $SysUser = SysUserService::instance();
    $ret = $SysUser->delete($ids);
    
    $this->ajaxReturn($ret);
  }
  
  function get_group() {
    $SysGroup = SysGroupService::instance() ;
    
    $group_id_arr = explode(',', $this->auth['group_id']);
    $html = '';
    
    foreach( $group_id_arr as $g_id ) {
      $data = $SysGroup->get_family( $g_id , self::SYS_TYPE);

      $html_one = $this->_get_group_html($data);
      $html .= preg_replace('/sub\-group/' , 'group' , $html_one ,1);
    }
    echo $html;
  }
  
  function _get_group_html( $data ) {

    $html = '<ul class="sub-group">';
    foreach ( $data as $row ) {
      $html .= '<li class="group-item">';
      $html .= '<div class="item-title" data-id="'. $row['id']. '"><i class="fa fa-square-o"></i> '. $row['name'] .'</div>';
      if ( isset( $row['son'] ) ) {
        $html .= $this->_get_group_html($row['son']) ;
      }
      $html .=  '</li>';
    }
    $html .= '</ul>';
    return $html ;
  }
  
  function reset_pwd( $user_id ) {
    $Auth = AuthService::instance();
    
    $ret = $Auth->reset_pwd($user_id);
    $this->ajaxReturn($ret);
  }
}
